package com.xwli.springcloud.controller;

import com.xwli.springcloud.service.AsyncTaskService;
import com.xwli.springcloud.service.impl.TestAsyncTaskOne;
import com.xwli.springcloud.service.impl.TestAsyncTaskTwo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private AsyncTaskService asyncTaskService;

    @PostMapping("/one")
    public ResponseEntity<String> testone(){
        try {
            ExecutorService executorService = Executors.newFixedThreadPool(2);
            TestAsyncTaskOne testAsyncTaskOne = new TestAsyncTaskOne();
            TestAsyncTaskTwo testAsyncTaskTwo = new TestAsyncTaskTwo();
            Future<String> testOne = executorService.submit(testAsyncTaskOne);
            Future<String> testTwo = executorService.submit(testAsyncTaskTwo);
            // 关闭线程池
            executorService.shutdown();
            String resultOne = testOne.get();
            log.info("方法一结果:{}",resultOne);
            String resultTwo = testTwo.get();
            log.info("方法二结果:{}",resultTwo);
        }catch (Exception e){
            log.error("异常:",e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
        return ResponseEntity.ok("成功");
    }

    @PostMapping("/two")
    public ResponseEntity<String> testTwo(){
        try {
            List<CompletableFuture> completableFutures = new ArrayList<>();
           /* CompletableFuture<String> testOne = asyncTaskService.testOne();
            CompletableFuture<String> testOne1 = asyncTaskService.testOne();
            CompletableFuture<String> testTwo = asyncTaskService.testTwo();
            CompletableFuture<Integer> testThree = asyncTaskService.testThree();*/
            CompletableFuture<Integer> testFour = asyncTaskService.testFour();
            CompletableFuture<Integer> testFour1 = asyncTaskService.testFour();
            CompletableFuture<Integer> testFour2 = asyncTaskService.testFour();
            CompletableFuture<Integer> testFour3 = asyncTaskService.testFour();
            completableFutures.add(testFour);
            completableFutures.add(testFour1);
            completableFutures.add(testFour2);
            completableFutures.add(testFour3);
            // 等待所有任务都执行完
            //CompletableFuture.allOf(testOne, testTwo).join();
            CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[completableFutures.size()])).join();
            log.info("方法一结果:{}",testFour.get());
            log.info("方法一.1结果:{}",testFour1.get());
            log.info("方法二结果:{}",testFour2.get());
            log.info("方法三结果:{}",testFour3.get());
        }catch (Exception e){
            log.error("异常:",e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
        return ResponseEntity.ok("成功");
    }

}
